﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Diploma.Models;

namespace DiplomaWeb.Controllers
{
    [Authorize(Roles = "SystemManager, DepartmentHead")]
    public class GlobalTasksCompletionsController : MasterController
    {
        public GlobalTasksCompletionsController(DataManager dataManager) : base(dataManager)
        {
        }
                
        public ActionResult SetCompletion(Guid globalTaskId, Guid studentId, bool? isCompleted)
        {
            GlobalTask globalTask = DataManager.GlobalTasksRepository.GetGlobalTask(globalTaskId);
            Student student = DataManager.StudentsRepository.GetStudent(studentId);
            if (globalTask == null || student == null)
                throw new Exception();

            DataManager.GlobalTasksRepository.SetCompletion(globalTask, student, isCompleted);

            GlobalTask nextTask = DataManager.GlobalTasksRepository.GetNextTasks(globalTask).FirstOrDefault();
            GlobalTask prevTask = (globalTask.PrevCheckPoint == null) ? null : globalTask.PrevCheckPoint as GlobalTask;

            var list = new List<GlobalTask> {nextTask, prevTask, globalTask};

            var var = (from gt in list.Where(item => item != null)
                       select new {GlobalTaskId = gt.CheckPointId, StudentId = studentId});
            return Json(var, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Get(Guid globalTaskId, Guid studentId)
        {
            GlobalTask globalTask = DataManager.GlobalTasksRepository.GetGlobalTask(globalTaskId);
            Student student = DataManager.StudentsRepository.GetStudent(studentId);
            GlobalTasksCompletion gtCompletion = DataManager.GlobalTasksCompletionsRepository.GetGlobalTaskCompletion(globalTask,
                                                                                                     student);
            if (globalTask == null || student == null)
                throw new Exception();

            ViewData["Student"] = student;
            ViewData.Model = globalTask;

            GlobalTask nextTask = DataManager.GlobalTasksRepository.GetNextTasks(globalTask).FirstOrDefault();
            GlobalTask prevTask = (globalTask.PrevCheckPoint == null) ? null : globalTask.PrevCheckPoint as GlobalTask;

            ViewData["ThisTaskCompletion"] = DataManager.GlobalTasksCompletionsRepository.GetCompletion(globalTask,
                                                                                                        student);
            ViewData["NextTaskCompletion"] = (nextTask == null)
                                                 ? null
                                                 : DataManager.GlobalTasksCompletionsRepository.GetCompletion(nextTask,
                                                                                                              student);
            ViewData["PreviousTaskCompletion"] = (prevTask == null)
                                                     ? null
                                                     : DataManager.GlobalTasksCompletionsRepository.GetCompletion(
                                                         prevTask, student);
            if (gtCompletion != null)
            {
                ViewData["CompletionDate"] = gtCompletion.CompletionDate;
            }
                return PartialView("~/Views/Planings/GlobalTaskCompletionControl.ascx");
        }

        public ActionResult GetAll(int LevelId) 
        {
            ViewData.Model = DataManager.QualificationLevelsRepository.GetAllLevelsWithStudents(LevelId);
            ViewData["PlaningTypes"] = DataManager.PlaningsRepository.GetAllPlaningTypes();

            return PartialView("~/Views/Planings/GlobalTasksCompletionsForAllStudentsControl.ascx"); 
        }
    }
}